function AnimatedSprite(texture, posX, posY, spriteHeight, spriteWidth, frameHeight, frameWidth, runOnce, speed, frames) {
    this.texture = texture;
    this.posX = posX;
    this.posY = posY;
    this.spriteHeight = spriteHeight;
    this.spriteWidth = spriteWidth;
    this.frameHeight = frameHeight;
    this.frameWidth = frameWidth;
    this.runOnce = runOnce;
    this._index = 0;
    this.speed = speed;
    this.frames = frames;
};

AnimatedSprite.prototype.update = function (dt) {
    this._index += this.speed * dt;
}

AnimatedSprite.prototype.render = function (ctx) {
    var frame;

    if (this.speed > 0) {
        var lastFrame = this.frames.length;
        var idx = Math.floor(this._index);
        frame = this.frames[idx % lastFrame];

        if (this.runOnce && idx >= lastFrame) {
            this.done = true;
            return;
        }
    } else {
        frame = 0;
    }

    var x = this.spriteWidth / this.frameWidth
    var y = this.spriteHeight / this.frameHeight

    x = frame * this.frameWidth;
    y = 0
    
    if(frame > 3)
    {
        y = 32;
        x = (frame - 4) * this.frameWidth;
    }

    ctx.drawImage(this.texture,
        x,
        y,
        32,
        32,
        this.posX,
        this.posY);
}

